<template>
  <div class="chat-container">
    <!-- 导航栏 -->
    <nav class="chat-header">
      <el-button
          type="info"
          @click="goBack"
          class="back-btn"
          :icon="ArrowLeftBold"
          circle
      />
      <div class="header-content">
        <div class="title-wrapper">
          <h1 class="title">AI Assistant</h1>
          <div class="ai-name">DeepSeek</div>
        </div>
        <div class="status-indicator">
          <el-icon class="online-dot"><SuccessFilled /></el-icon>
          <span>Online</span>
        </div>
      </div>
    </nav>

    <!-- 消息区域 -->
    <div class="chat-messages" ref="messagesContainer">
      <transition-group name="message-fade">
        <div
            v-for="(msg, index) in messages"
            :key="index"
            :class="['message-item', msg.type]"
        >
          <div class="message-content">
            <!-- 机器人头像 -->
            <div v-if="msg.type === 'bot'" class="avatar">
              <el-avatar :size="40">
                <el-icon><UserFilled /></el-icon>
              </el-avatar>
            </div>

            <!-- 消息卡片 -->
            <el-card
                shadow="hover"
                :class="['message-bubble', msg.type]"
                :body-style="{ padding: '12px 16px' }"
            >
              <template #header>
                <div class="message-header">
                  <span class="sender">{{ msg.type === 'user' ? 'You' : 'DeepSeek' }}</span>
                  <span class="timestamp">{{ msg.timestamp }}</span>
                </div>
              </template>
              <div class="message-text">
                <span v-if="msg.type === 'error'" class="error-text">
                  <el-icon><Warning /></el-icon>
                  {{ msg.content }}
                </span>
                <template v-else>{{ msg.content }}</template>
              </div>
            </el-card>
          </div>
        </div>
      </transition-group>

      <!-- 加载状态 -->
      <div v-if="isLoading" class="loading-wrapper">
        <div class="loading-indicator">
          <el-icon class="loading-icon"><Loading /></el-icon>
          <span class="loading-text">Thinking...</span>
        </div>
      </div>
    </div>

    <!-- 输入区域 -->
    <div class="chat-input-wrapper">
      <div class="chat-input">
        <el-input
            v-model="inputMessage"
            type="textarea"
            :rows="2"
            placeholder="Type your message..."
            @keyup.enter.native="sendMessage"
            :disabled="isLoading"
            class="message-input"
            resize="none"
        />
        <el-button
            type="primary"
            @click="sendMessage"
            :loading="isLoading"
            :disabled="!inputMessage.trim()"
            class="send-btn"
            round
        >
          <template #icon>
            <el-icon><Promotion /></el-icon>
          </template>
          Send
        </el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, nextTick } from 'vue';
import { useRouter } from 'vue-router';
import {
  ArrowLeftBold,
  UserFilled,
  Loading,
  SuccessFilled,
  Warning,
  Promotion,
} from '@element-plus/icons-vue';
import axios from 'axios';

export default {
  components: {
    ArrowLeftBold,
    UserFilled,
    Loading,
    SuccessFilled,
    Warning,
    Promotion,
  },
  setup() {
    const router = useRouter();
    const messages = ref([]);
    const inputMessage = ref('');
    const isLoading = ref(false);
    const messagesContainer = ref(null);

    const formatTime = () => {
      const now = new Date();
      return now.toLocaleTimeString([], {hour: '2-digit', minute: '2-digit'});
    };

    const goBack = () => {
      router.go(-1);
    };

    const scrollToBottom = async () => {
      await nextTick();
      if (messagesContainer.value) {
        messagesContainer.value.scrollTo({
          top: messagesContainer.value.scrollHeight,
          behavior: 'smooth',
        });
      }
    };

    const sendMessage = async () => {
      const userMessage = inputMessage.value.trim();
      if (!userMessage) return;

      messages.value.push({
        type: 'user',
        content: userMessage,
        timestamp: formatTime(),
      });

      inputMessage.value = '';
      isLoading.value = true;

      try {
        const response = await axios.post('/api/chat', {
          message: userMessage,
        });

        messages.value.push({
          type: 'bot',
          content: response.data.response,
          timestamp: formatTime(),
        });
      } catch (error) {
        console.error('Error:', error);
        messages.value.push({
          type: 'error',
          content: 'Oops! Something went wrong. Please try again.',
          timestamp: formatTime(),
        });
      } finally {
        isLoading.value = false;
        scrollToBottom();
      }
    };

    return {
      messages,
      inputMessage,
      isLoading,
      messagesContainer,
      sendMessage,
      goBack,
    };
  },
};
</script>

<style scoped>
:root {
  --primary-color: #5B8FF9;
  --secondary-color: #7FBA7A;
  --accent-color: #FF9F43;
  --bg-color: #f8f9fa;
  --card-bg: rgba(255, 255, 255, 0.95);
  --text-primary: #2c3e50;
  --text-secondary: #7f8c8d;
  --success-color: #28aa46;
  --danger-color: #dc3545;
}

.chat-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: linear-gradient(135deg, var(--bg-color) 0%, #e9ecef 100%);
}

.chat-header {
  display: flex;
  align-items: center;
  padding: 12px 24px;
  background: rgba(255, 255, 255, 0.8);
  backdrop-filter: blur(10px);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 1000;
}

.back-btn {
  margin-right: 20px;
  transition: transform 0.2s;
}

.back-btn:hover {
  transform: translateX(-3px);
}

.title-wrapper {
  display: flex;
  flex-direction: column;
  line-height: 1.2;
}

.title {
  margin: 0;
  font-size: 1.8rem;
  font-weight: 600;
  color: var(--text-primary);
  letter-spacing: -0.5px;
}

.ai-name {
  font-size: 0.9rem;
  color: var(--primary-color);
  font-weight: 600;
  letter-spacing: 1px;
  background: rgba(255, 255, 255, 0.9);
  padding: 4px 8px;
  border-radius: 6px;
  display: inline-block;
  margin-top: 4px;
}

.status-indicator {
  display: flex;
  align-items: center;
  font-size: 0.9rem;
  color: var(--text-secondary);
  position: relative;
  padding-left: 24px;
}

.online-dot {
  position: absolute;
  left: 0;
  color: var(--success-color) !important; /* 强制使用绿色 */
  animation: pulse 1.5s infinite;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px 15% 120px; /* 增加底部 padding 避免输入栏遮挡 */
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.message-fade-enter-active,
.message-fade-leave-active {
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.message-fade-enter-from,
.message-fade-leave-to {
  opacity: 0;
  transform: translateY(20px);
}

.message-item {
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.message-content {
  display: flex;
  gap: 12px;
  max-width: 75%;
  transition: transform 0.3s;
}

.message-item.user .message-content {
  margin-left: auto;
  justify-content: flex-end;
}

.message-item.bot .message-content {
  margin-right: auto;
  justify-content: flex-start;
}

.message-bubble {
  border-radius: 20px !important;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  will-change: transform, box-shadow;
}

.message-bubble.bot {
  background: var(--card-bg);
  border: none;
  border-radius: 20px 20px 20px 4px !important;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.message-bubble.user {
  background: linear-gradient(135deg, var(--primary-color) 0%, #6C5CE7 100%) !important;
  border: 2px solid rgba(255, 255, 255, 0.3) !important;
  border-radius: 20px 20px 4px 20px !important;
  box-shadow: 0 4px 20px rgba(91, 143, 249, 0.4),
  0 0 10px rgba(255, 255, 255, 0.2) inset;
  position: relative;
}

.message-bubble.user::after {
  content: '';
  position: absolute;
  top: -2px;
  left: -2px;
  right: -2px;
  bottom: -2px;
  border-radius: 22px;
  border: 2px solid rgba(255, 255, 255, 0.4);
  animation: borderGlow 2s ease-in-out infinite;
  opacity: 0.6;
  z-index: -1;
}

@keyframes borderGlow {
  0% {
    opacity: 0.4;
    filter: brightness(1);
  }
  50% {
    opacity: 0.8;
    filter: brightness(1.2);
  }
  100% {
    opacity: 0.4;
    filter: brightness(1);
  }
}

.message-item.user:hover .message-bubble.user {
  box-shadow: 0 6px 24px rgba(91, 143, 249, 0.6),
  0 0 15px rgba(255, 255, 255, 0.3) inset;
  transform: translateY(-2px) scale(1.02);
}

.sender {
  font-size: 0.9rem;
  font-weight: 600;
  letter-spacing: 0.5px;
}

.timestamp {
  font-size: 0.75rem;
  opacity: 0.8;
}

.message-text {
  font-size: 1rem;
  line-height: 1.6;
  word-break: break-word;
}

.error-text {
  color: var(--danger-color);
  display: flex;
  align-items: center;
  gap: 6px;
}

.loading-wrapper {
  padding: 20px 0;
}

.loading-indicator {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  color: var(--text-secondary);
}

.loading-icon {
  font-size: 1.5rem;
  animation: rotating 2s cubic-bezier(0.68, -0.55, 0.27, 1.55) infinite;
}

.loading-text {
  background: linear-gradient(90deg, #666, #999, #666);
  background-size: 200% 100%;
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  animation: shimmer 2s linear infinite;
}

.chat-input-wrapper {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 20px 15%;
  background: linear-gradient(180deg, rgba(248, 249, 250, 0) 0%, rgba(248, 249, 250, 0.9) 30%);
}

.chat-input {
  background: var(--card-bg);
  backdrop-filter: blur(8px);
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s;
  padding: 16px;
  display: flex;
  gap: 12px;
}

.chat-input:hover {
  transform: translateY(-2px);
}

.message-input {
  flex: 1;
}

.send-btn {
  background: linear-gradient(135deg, var(--primary-color) 0%, #6C5CE7 100%);
  border: none;
  padding: 12px 28px;
  transition: all 0.3s;
  height: 56px;
  margin-left: auto;
}

.send-btn:hover {
  box-shadow: 0 4px 16px rgba(91, 143, 249, 0.3);
  transform: translateY(-2px);
}

@keyframes rotating {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

@keyframes shimmer {
  0% {
    background-position: 200% 0;
  }
  100% {
    background-position: -200% 0;
  }
}

@keyframes pulse {
  0% {
    opacity: 0.6;
    transform: scale(0.95);
  }
  50% {
    opacity: 1;
    transform: scale(1);
  }
  100% {
    opacity: 0.6;
    transform: scale(0.95);
  }
}

@media (max-width: 768px) {
  .chat-messages,
  .chat-input-wrapper {
    padding: 15px;
  }

  .message-content {
    max-width: 90%;
  }

  .title {
    font-size: 1.4rem;
  }

  .chat-input {
    flex-direction: column;
    gap: 12px;
  }

  .send-btn {
    width: 100%;
    height: auto;
  }

  .message-bubble.user::after {
    animation-duration: 3s;
  }

  .message-item.user:hover .message-bubble.user {
    transform: none;
  }
}
</style>